package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.JzdxProbationOffenderEscortConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxProbationOffenderEscortCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxProbationOffenderEscortQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxProbationOffenderEscortResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxProbationOffenderEscortUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxProbationOffenderEscortEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxProbationOffenderEscortMapper;
import com.engine4cloud.tdf.supervision.service.JzdxProbationOffenderEscortService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 暂予监外执行罪犯押送信息表
 *
 * @author wenjie
 * @date 2024-04-01 14:24:28
 */
@Service
public class JzdxProbationOffenderEscortServiceImpl extends ServiceImpl<JzdxProbationOffenderEscortMapper, JzdxProbationOffenderEscortEntity> implements JzdxProbationOffenderEscortService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<JzdxProbationOffenderEscortResultDTO> pageList(Params pageable, JzdxProbationOffenderEscortQueryDTO dto) {
        Page<JzdxProbationOffenderEscortEntity> page = page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, JzdxProbationOffenderEscortConvert.INSTANCE::convert);
    }

    @Override
    public JzdxProbationOffenderEscortResultDTO getById(Long id) {
        return JzdxProbationOffenderEscortConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(JzdxProbationOffenderEscortCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(JzdxProbationOffenderEscortConvert.INSTANCE.create(dto, dx.getTenantId()));
    }

    @Override
    public Boolean update(JzdxProbationOffenderEscortUpdateDTO dto) {
        JzdxProbationOffenderEscortEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(JzdxProbationOffenderEscortConvert.INSTANCE.update(dto));
    }

    private LambdaQueryWrapper<JzdxProbationOffenderEscortEntity> wrapper(JzdxProbationOffenderEscortQueryDTO query) {
        LambdaQueryWrapper<JzdxProbationOffenderEscortEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(JzdxProbationOffenderEscortEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(JzdxProbationOffenderEscortEntity::getRybh, v));
        Queries.accept(query.getYsrq(), v -> wrapper.eq(JzdxProbationOffenderEscortEntity::getYsrq, v));
        Queries.accept(query.getZxysjcxm(), v -> wrapper.like(JzdxProbationOffenderEscortEntity::getZxysjcxm, v));
        return wrapper;
    }
}